<template>
	<!--在线会话-->
	<view class="session_page">
		<scroll-view class="session_list" scroll-y>
			<block v-if="sessionList && sessionList.length > 0">
				<view :key="index" v-for="(item,index) in sessionList" class="user_item"
					:data-index="index" @touchstart="drawStart($event)" @touchmove="drawMove($event)"
					@touchend="drawEnd($event)" :style="{ right: item.RightPadding + 'rpx' }">
					<view class="user_content" @click="selectSession(item)">
						<view class="user_left">
							<image :src="item.Avatar"></image>
							<view v-if="getUnReadCount(item.FormId) > 0" class="un_read">
								{{getUnReadCount(item.FormId)}}
							</view>
						</view>
						<view class="user_right">
							<view class="intername">{{ item.Name }}</view>
							<view class="infor overflow_1" v-html="getLastSession(item.FormId)"></view>
						</view>
					</view>
					<view class="remove" @click.stop="delItem(item)">删除</view>
				</view>
			</block>
			<view v-else class="no_session">
				<view>
					<image class="no_session_img" src="../../static/img/noPeple.png" mode="widthFix">
					</image>
					<view>正等待被撩...</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				startX: 0,
				isScroll: true,
				delBtnWidth: 160,
				sessionList:this.$store.state.sessionList
			}
		},
		watch:{
			"$store.state.sessionList":{
				deep:true,
				handler:function(newV){
					console.log("更新");
					this.sessionList = newV
				}
			}
		},
		computed: {
			getLastSession() {
				return (id) => {
					let currentContent = this.$store.state.conversitionList.filter(
						(x) =>
						(x.SendId == this.$store.state.sender.Id && x.ReciverId == id) ||
						(x.SendId == id && x.ReciverId == this.$store.state.sender.Id)
					);
					let result = "";
					if (currentContent.length > 0) {
						let len = currentContent.length - 1;
						switch (currentContent[len].Type) {
							case 0:
								result = currentContent[len].Content;
								break;
							case 1:
								result = "图片";
								break;
							case 2:
								result = "视频";
								break;
							case 3:
								result = "语音";
								break;
						}
					}
					return result;
				};
			},
			getUnReadCount() {
				return (id) => {
					let currentContent = this.$store.state.conversitionList.filter(
						(x) =>
						x.SendId == id && x.ReciverId == this.$store.state.sender.Id && !x.ReadFlag
					);
					return currentContent.length > 999 ? "999+" : currentContent.length;
				}
			}
		},
		methods: {
			//删除在线会话
			delItem(session) {
				console.log("删除会话");
			},
			selectSession(item){
				this.$store.commit("setPropName",{propName:'sessionSelectId',value:item.FormId});
				this.$store.commit("setPropName",{propName:'reciver',value:item});
				this.$Common.toBottom();
				this.$Common.changeReaded(item.FormId);
				this.$Common.navigateTo("/chatContent/chatContent");
			},
			drawStart: function(e) {
				let touch = e.touches[0];
				for (let index in this.$store.state.sessionList) {
					this.$store.state.sessionList[index].RightPadding = 0;
				}
				this.startX = touch.clientX;
			},
			drawMove: function(e) {
				let touch = e.touches[0];
				let disX = this.startX - touch.clientX;
				let sessionList = this.$store.state.sessionList;
				if (disX >= 20) {
					if (disX > this.delBtnWidth) {
						disX = this.delBtnWidth;
					}
					this.isScroll = false;
					sessionList[e.currentTarget.dataset.index].RightPadding = disX;
				} else {
					this.isScroll = true;
					sessionList[e.currentTarget.dataset.index].RightPadding = 0;
				}
				this.$store.commit('setPropName', {
					propName: 'sessionList',
					value: sessionList
				})
			},
			drawEnd: function(e) {
				let sessionList = this.$store.state.sessionList;
				let item = sessionList[e.currentTarget.dataset.index];
				if (item.RightPadding >= this.delBtnWidth / 2) {
					this.isScroll = true;
					sessionList[e.currentTarget.dataset.index].RightPadding = this.delBtnWidth;
				} else {
					this.isScroll = true;
					sessionList[e.currentTarget.dataset.index].RightPadding = 0;
				}
				this.$store.commit('setPropName', {
					propName: 'sessionList',
					value: sessionList
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.session_page {
		height: 100%;
		width: 100%;

		.session_list {
			height: 100%;
			width: 100%;
			overflow: auto;

			.user_item {
				height: 150rpx;
				width: 100%;
				position: relative;
				box-sizing: border-box;

				.user_content {
					height: 100%;
					width: 100%;
					display: flex;

					.user_left {
						width: 18%;
						height: 100%;
						display: flex;
						justify-content: center;
						align-items: center;
						position: relative;
						image {
							height: 100rpx;
							width: 100rpx;
						}
						.un_read {
							display: inline-block;
							position: absolute;
							top: 8px;
							right: 2px;
							height: 20px;
							width: 20px;
							border-radius: 50%;
							line-height: 10px;
							padding: 5px;
							text-align: center;
							background: #EE593C;
							color: #fff;
							font-size: 12px;
						}
					}

					.user_right {
						width: 82%;
						height: 100%;
						border-bottom: 1px solid #EFEFEF;
						text-align: left;

						.intername {
							height: 40px;
							line-height: 40px;
						}

						.infor {
							font-size: 12px !important;
							color: #707070;
							width: 98%;
							overflow: hidden;
							text-overflow: ellipsis;
						}
					}
				}

				.remove {
					width: 160rpx;
					height: 100%;
					background-color: #FF5252;
					color: white;
					position: absolute;
					top: 0;
					right: -160rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					box-sizing: border-box;
				}
			}

			.no_session {
				height: 80rpx;
				line-height: 80rpx;
				width: 100%;
				text-align: center;
				font-size: 34rpx;
				color: #C8C7CC;

				.no_session_img {
					width: 200rxp;
					height: auto;
				}
			}

		}
	}
</style>
